﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using System;
using System.IO;
using UEditor.Core;

namespace WebApplication1.Controllers
{
    [Route("/[controller]/[action]")]
    public class UEditorController : Controller
    {
        private readonly UEditorService _ueditorService;
        public UEditorController(UEditorService ueditorService)
        {
            this._ueditorService = ueditorService;
        }
  


        [HttpGet, HttpPost]
        //[HttpPost("upload")]
        [RequestSizeLimit(810_000_000)] //最大100m左右
        public ContentResult Upload()
        {
            string url = string.Empty;

            
            var response = _ueditorService.UploadAndGetResponse(HttpContext);
            //var act =  HttpContext.Request.Query["action"];
            //if (!act.IsNullOrEmpty())
            //{
            //    string action = act.ToString();
            //    switch (action) {
            //        case ("uploadimage"): {
            //                var toLibary = HttpContext.Request.Form["ToLibary"].ToString();
            //                var _listcls = (JObject)JsonConvert.DeserializeObject(response.Result);
            //                var imgPath = _listcls["url"].ToString();
            //                break; 
            //            }
            //        case ("uploadvideo"):
            //            {
            //                var toLibary = HttpContext.Request.Form["ToLibary"].ToString();
            //                var _listcls = (UData)JsonConvert.DeserializeObject(response.Result);
            //                var imgPath = _listcls.url;
            //                break;
            //            }
            //        case ("uploadfile"):
            //            {
            //                var toLibary = HttpContext.Request.Form["ToLibary"].ToString();
            //                var _listcls = (UData)JsonConvert.DeserializeObject(response.Result);
            //                var videoPath = _listcls.url;
            //                break;
            //            }
            //        case ("uploadmusic"):
            //            {
            //                var toLibary = HttpContext.Request.Form["ToLibary"].ToString();
            //                var _listcls = (UData)JsonConvert.DeserializeObject(response.Result);
            //                var filePath = _listcls.url;
            //                break;
            //            }
            //    }

            //}



            //int s1 = response.Result.IndexOf("(");
            ////string str1 = response.Result.Replace("bd__editor__cv6ck6(","");
            //int s2 = response.Result.IndexOf(");");
            //var str = response.Result.Substring(s1, s2 - s2 + 2);
            ////将json转化为实体对象
            //UeditorUrl model = JsonConvert.DeserializeObject<UeditorUrl>(response.Result);          
            //model.snapscreenUrlPrefix = url;
            //model.imageUrlPrefix = url;
            //model.catcherUrlPrefix = url;
            //model.videoUrlPrefix = url;
            //model.fileUrlPrefix = url;
            //model.imageManagerUrlPrefix = url;
            //model.fileManagerUrlPrefix = url;
            //model.scrawlUrlPrefix = url;
            //string aa = model.ToJson();//将实体对象转化为json
             Log.Information(response.Result);
             var q = Content(response.Result, response.ContentType);         
            //return Content(aa, response.ContentType);
            return q;
        }

        [HttpPost]
        public IActionResult New([FromServices] IWebHostEnvironment env)
        {
            var fileName = Path.Combine("upload");
            var stream = new FileStream(Path.Combine(env.WebRootPath, fileName), FileMode.CreateNew);
            return RedirectToAction(nameof(Index));
        }

        private class UData { 
            public string state { set; get;  }
            public string url { set; get; }
            public string title { set; get; }
            public string original { set; get; }

        }
    }
}